﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Sql;

public partial class Manage_MageInf : System.Web.UI.Page
{
  
    string username;
    string password;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bool loginFlag = false;
            HttpCookie usr = Request.Cookies["ManagerUsr"];
            HttpCookie psw = Request.Cookies["ManagerPsw"];
            if (usr != null && psw != null)
            {
                loginFlag = checkLogin(usr.Value, psw.Value);
            }
            if (loginFlag == true)
            {
                fullFillData();
            }
            else
            {
                Response.Redirect("./MageLogin.aspx");
            }
        }

    }

    protected bool checkLogin(string usr, string psw)
    {
        if ((Master.FindControl("endLoginBut") as Button).Visible == true)
            return true;
        else
            return false;
    }
    private void fullFillData()
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrManager"].ConnectionString;
        HttpCookie usr = Request.Cookies["ManagerUsr"];        
        string Sno = usr.Value;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();

                String sqlString = "select * from Manager_Staff where Sno = @Sno";
                SqlParameter[] parameters = { new SqlParameter("@Sno", Sno) };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataReader dr = cmd.ExecuteReader();
                    dr.Read();

                    SnoTex.Text = dr["Sno"].ToString();
                    NameTex.Text = dr["Sname"].ToString();
                    SduteTex.Text = dr["Sdute"].ToString();
                    if (Convert.ToBoolean(dr["Ssex"]) == true)
                        SexRadioList.SelectedValue = "1";
                    else
                        SexRadioList.SelectedValue = "0";
                    PhoneTex.Text = long.Parse(dr["Sphone"].ToString()).ToString();
                    BirthTex.Text = dr["Sbirth"].ToString();
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }


    }
    protected void ResetBut_Click(object sender, EventArgs e)
    {
        fullFillData();
    }
    protected void UpdateBut_Click(object sender, EventArgs e)
    {
        saveData();
    }

    private void saveData()
    {

        bool sex = true;
        if (SexRadioList.SelectedValue == "1")
            sex = true;
        else
            sex = false;
        string tel = PhoneTex.Text;
        string birth = PhoneTex.Text;
        string name = NameTex.Text;
        string Sno;
        HttpCookie usr = Request.Cookies["ManagerUsr"];
        Sno = usr.Value;

        if (checkData(name, tel,birth) == false)
        {
            Response.Write(@"<script>alert('信息填写不符合要求！');</script>");
            return;
        }
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrManager"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                //String sqlString = "select * from Customer_Account where Aaccount = @username";
                String sqlString = "update Manager_Staff set Sname = @name  , Ssex= @sex,  Sphone = @tel  where Sno = @Sno";
                SqlParameter[] parameters = { 
                                                new SqlParameter("@name", name),
                                                new SqlParameter("@sex", sex),
                                                new SqlParameter("@tel", tel),                                                
                                                new SqlParameter("@Sno", Sno)
                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                        Response.Write(@"<script>alert('修改成功');</script>");
                        Response.AddHeader("Refresh", "0");  //刷新当前页面
                    }
                    else
                    {
                        Response.Write(@"<script>alert('修改失败');</script>");
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    private bool checkData(string name, string tel, string birth)
    {
        bool ans = true;
        if (!(name.Length > 0))
            ans = false;
        else if (tel.Length > 11)
            ans = false;
        else if (birth.Length > 100)
            ans = false;
        return ans;
    }
}